<?php
//登入注册API控制器
namespace app\api\controller;

//TP请求模块
use think\facade\Request;
//TP验证模块
use think\exception\ValidateException;
//TP验证模块
use think\facade\Validate;
//TPCache模块
use think\facade\Cache;

//AppProjectArea模型
use app\api\model\AppProjectArea as AppProjectAreaModel;
//project模型
use app\api\model\Project as ProjectModel;
//project验证器
use app\api\validate\Project as ProjectValidate;
//user模型
use app\api\model\User as UserModel;
//user验证器
use app\api\validate\User as UserValidate;
//公共类
use app\api\common\Common;


class Project
{

    //获取商店项目列表
    public function getProjectList()
    {
        //整理数据
        $nowPage = Request::param('page');
        if (empty($nowPage)) {
            $nowPage = 1;
        }

        //查询数据
        $request = ProjectModel::where('show_state', 'true')->where('state', 'true')->field('id, name, price, image');

        $numPage = 12; //每页个数
        $lastPage = ceil($request->count() / $numPage); //末页页数

        $request = $request->limit(($nowPage - 1) * $numPage, $numPage)->select(); //输出分页数据

        $data = array(
            'nowPage' => (int)$nowPage,
            'lastPage' => (int)$lastPage,
            'data' => $request
        );

        //判断返回数据
        if ($request->isEmpty()) {
            return Common::create($data, '数据不存在', 204);
        } else {
            return Common::create($data, '数据请求成功', 200);
        }
    }

    //获取项目分区
    public function getProjectArea()
    {
        //查询数据
        $request = AppProjectAreaModel::select();
        $data = $request;

        //判断返回数据
        if ($request->isEmpty()) {
            return Common::create($data, '数据不存在', 204);
        } else {
            return Common::create($data, '数据请求成功', 200);
        }
    }

    //获取项目详情
    public function getProject()
    {
        //整理数据
        $id = Request::param('id');

        //查询数据
        $request = ProjectModel::where('id', $id)->where('state', 'true')->findOrEmpty();
        $data['project'] = $request;

        $requests = UserModel::where('uid', $request['uid'])->field('username, avatar')->findOrEmpty();
        $data['user'] = $requests;

        //判断返回数据
        if ($request->isEmpty()) {
            return Common::create($data, '数据不存在', 204);
        } else {
            return Common::create($data, '数据请求成功', 200);
        }
    }

    //添加项目
    public function postProject()
    {
        //整理数据
        $uid = Common::getUid();

        $name = Request::param('name');
        $area = Request::param('area');
        $brief_introduce = Request::param('brief_introduce');
        $show_state = 'false';
        $state = 'true';

        //验证参数是否合法
        try {
            validate(ProjectValidate::class)->batch(true)
                ->scene('ProjectAdd')
                ->check([
                    'name' => $name,
                    'area' => $area,
                    'brief_introduce' => $brief_introduce,
                ]);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            $error = $e->getError();
            return Common::create($error, '数据添加失败', 400);
        }

        //静态方法写入数据
        $request = ProjectModel::create([
            'uid'  =>  $uid,
            'name' =>  $name,
            'area'  =>  $area,
            'brief_introduce'  =>  $brief_introduce,
            'show_state'  =>  $show_state,
            'state'  =>  $state,
            'release_date'  =>  date('Y-m-d h:i:s', time()),
            'update_date'  =>  '2022-01-01 00:00:00'
        ]);

        //判断返回数据
        if ($request->isEmpty()) {
            return Common::create([], '数据添加失败', 204);
        } else {
            return Common::create([], '数据添加成功', 200);
        }
    }

    //编辑项目
    public function putProject()
    {
        //整理数据
        $uid = Common::getUid();

        $id = Request::param('id');
        $name = Request::param('name');
        $image = Request::param('image');
        $introduce = Request::param('introduce');
        $brief_introduce = Request::param('brief_introduce');
        $show_state = Request::param('show_state');

        //验证参数是否合法
        try {
            validate(ProjectValidate::class)->batch(true)
                ->scene('ProjectEdit')
                ->check([
                    'name' => $name,
                    'image' => $image,
                    'brief_introduce' => $brief_introduce,
                    'introduce' => $introduce,
                    'show_state' => $show_state,
                ]);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            $error = $e->getError();
            return Common::create($error, '数据修改失败', 400);
        }

        //静态方法写入数据   
        $request = ProjectModel::update([
            'name' =>  $name,
            'image' => $image,
            'brief_introduce'  =>  $brief_introduce,
            'introduce' => $introduce,
            'show_state'  =>  $show_state,
            'update_date'  =>  date('Y-m-d h:i:s', time())
        ], ['id' => $id]);

        //判断返回数据
        if ($request->isEmpty()) {
            return Common::create([], '数据修改失败', 204);
        } else {
            return Common::create([], '数据修改成功', 200);
        }
    }

    //删除项目
    public function deletProject()
    {
        //整理数据
        $uid = Common::getUid();
        $id = Request::param('id');

        $request = ProjectModel::where('uid', $uid)->where('id', $id)->where('state', 'true')->delete();

        //判断返回数据
        if (!$request) {
            return Common::create([], '数据删除失败', 204);
        } else {
            return Common::create([], '数据删除成功', 200);
        }
    }
}
